package com.panshi.controller.question;

import com.panshi.DiscussPage.PageBeanVo;
import com.panshi.api.IDiscussApi;
import com.panshi.bean.PageBean;
import com.panshi.po.DiscussPO;
import com.panshi.po.QuestionPO;
import com.panshi.vo.PageModel;
import com.panshi.vo.QueryDimVO;
import com.panshi.vo.ResponseVO;
import com.panshi.vo.UpdateDiscussVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class AnswerDiscussController {

    @Autowired
    IDiscussApi iDiscussApi;

    @GetMapping("answer-discuss-home")
    public String queryHome(){
        return "answerDiscuss/discuss-home";
    }

    @PostMapping("discuss-queryAll")
    @ResponseBody
    public PageModel<DiscussPO> queryAll(PageModel<DiscussPO> pageModel){
        PageBeanVo pageBeanVo = new PageBeanVo();
        pageBeanVo.setPageSize(pageModel.getPageSize());
        pageBeanVo.setCurrentPage(pageModel.getCurrentPage());
        List<DiscussPO> list=iDiscussApi.queryDiscussPostBody(pageBeanVo).getData();
        Integer integer=iDiscussApi.queryNumber().getData();
        pageModel.setList(list);
        pageModel.setCount(integer);
        return pageModel;
    }

    @PostMapping("discuss-likeQuery/{content}")
    @ResponseBody
    public PageModel<DiscussPO> update(@PathVariable String content,PageModel<DiscussPO> pageModel){
        log.info("content==={}",content);
        QueryDimVO queryDimVO=new QueryDimVO();
        queryDimVO.setContent(content);
        queryDimVO.setCurrentPage(pageModel.getCurrentPage());
        queryDimVO.setPageSize(pageModel.getPageSize());
        List<DiscussPO> discussPO =iDiscussApi.queryDimDiscussPostBody(queryDimVO).getData();
        Integer integer= iDiscussApi.queryDimNumber(content).getData();
        pageModel.setList(discussPO);
        pageModel.setCount(integer);
        return pageModel;
    }

    @PostMapping("discuss-delete")
    @ResponseBody
    public Map<String,Object> delete(UpdateDiscussVO updateDiscussVO){
        log.info("updateDiscussVO=={}",updateDiscussVO);

        Integer integer=iDiscussApi.updateDiscussPostBody(updateDiscussVO).getData();

        Map<String,Object> map = new HashMap<>();
        if(integer == 1){
            map.put("success",true);
            map.put("message","删除成功");
        }else{
            map.put("success",false);
            map.put("message","删除失败");
        }
        return map;
    }

    @GetMapping("idQuery/{id}")
    @ResponseBody
    public ModelAndView update(@PathVariable Integer id){
        ModelAndView mv = new ModelAndView("answerDiscuss/discuss-edit");
        DiscussPO discussPO =iDiscussApi.queryId(id).getData();
        mv.addObject("discussPO",discussPO);
        return mv;
    }

    @PostMapping("discuss-update")
    @ResponseBody
    public ResponseVO update(UpdateDiscussVO updateDiscussVO){
        log.info("updateDiscussVO=={}",updateDiscussVO);

        Integer integer=iDiscussApi.updateDiscussPostBody(updateDiscussVO).getData();
        if(integer == 1){
            return new ResponseVO("200","修改成功",true);
        }
        return new ResponseVO("500","修改失败",false);
    }


}
